(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(*-----------------------------------------------------------------------*)

open Pycaml
  
type num = float (* with sexp *)

let description = "floating point"
let module_name = "float"

(*-----------------------------------------------------------------------*)

let num_of_int = float_of_int
let num_of_ints x y = (float_of_int x) /. (float_of_int y)
let float_of_num x = x
let string_of_num = string_of_float
let num_zero = 0.0
let num_one = 1.0
let num_minus_one = -. 1.0
let num_two = 2.0
let num_three = 3.0
let num_ten = 10.0
let add_num x y = x +. y
let minus_num x = -. x
let sub_num x y = x -. y
let mult_num x y = x *. y
let div_num x y = x /. y
let sign_num x = if x < 0.0 then (-1) else if x > 0.0 then 1 else 0
let compare_num x y = compare x y
let square_num x = x *. x
let is_integer_num x = x = (floor x)
let power_num_int x exp = x ** (float_of_int exp)
let power_num x y = x ** y
let abs_num = abs_float
let succ_num x = x +. 1.0
let pred_num x = x -. 1.0
let incr_num x = x := succ_num !x
let decr_num x = x := pred_num !x
let floor_num = floor
let ceiling_num = ceil
  
let integer_num x =
  let (f, z) = modf x in
    if f > 0.5 then
      z +. 1.0
    else if f < (-0.5) then
      z -. 1.0
    else
      z
  
let round_num x =
  let (f, z) = modf x in
    if f >= 0.5 then
      z +. 1.0
    else if f <= (-0.5) then
      z -. 1.0
    else
      z
  
let quo_num x y = floor_num (div_num x y)
let mod_num x y = sub_num x (mult_num y (quo_num x y))
let eq_num x y = x = y
let lt_num x y = x < y
let le_num x y = x <= y
let gt_num x y = x > y
let ge_num x y = x >= y
let max_num x y = max x y
let min_num x y = min x y
let land_num x y = float_of_int ((int_of_float x) land (int_of_float y))
let lor_num x y = float_of_int ((int_of_float x) lor (int_of_float y))
let lxor_num x y = float_of_int ((int_of_float x) lxor (int_of_float y))
let lneg_num x = float_of_int (lnot (int_of_float x))

let num_of_string s =
  try
    let n = String.index s '/' in
      (float_of_string (String.sub s 0 n)) /.
        (float_of_string (String.sub s (n + 1) (((String.length s) - n) - 1)))
  with | Not_found -> float_of_string s

let int_of_num = int_of_float
let num_of_float x = x

let serialise_num output x =
  let z = Int64.bits_of_float x in
  let mask = Int64.of_int 0xff in
  let b0 = Int64.logand z mask in
  let b1 = Int64.logand (Int64.shift_right_logical z 8) mask in
  let b2 = Int64.logand (Int64.shift_right_logical z 16) mask in
  let b3 = Int64.logand (Int64.shift_right_logical z 24) mask in
  let b4 = Int64.logand (Int64.shift_right_logical z 32) mask in
  let b5 = Int64.logand (Int64.shift_right_logical z 40) mask in
  let b6 = Int64.logand (Int64.shift_right_logical z 48) mask in
  let b7 = Int64.logand (Int64.shift_right_logical z 56) mask in
    output (char_of_int (Int64.to_int b7));
    output (char_of_int (Int64.to_int b6));
    output (char_of_int (Int64.to_int b5));
    output (char_of_int (Int64.to_int b4));
    output (char_of_int (Int64.to_int b3));
    output (char_of_int (Int64.to_int b2));
    output (char_of_int (Int64.to_int b1));
    output (char_of_int (Int64.to_int b0))
    
let unserialise_num input =
  let b7 = int_of_char (input ()) in
  let b6 = int_of_char (input ()) in
  let b5 = int_of_char (input ()) in
  let b4 = int_of_char (input ()) in
  let b3 = int_of_char (input ()) in
  let b2 = int_of_char (input ()) in
  let b1 = int_of_char (input ()) in
  let b0 = int_of_char (input ()) in
  let x0 = Int64.of_int (b0 lor (b1 lsl 8)) in
  let x1 = Int64.of_int (b2 lor (b3 lsl 8)) in
  let x2 = Int64.of_int (b4 lor (b5 lsl 8)) in
  let x3 = Int64.of_int (b6 lor (b7 lsl 8)) in
  let y0 = Int64.logor x0 (Int64.shift_left x1 16) in
  let y1 = Int64.logor x2 (Int64.shift_left x3 16) in
    Int64.float_of_bits (Int64.logor y0 (Int64.shift_left y1 32))

(*-----------------------------------------------------------------------*)

let pythonize_num = pyfloat_fromdouble
let unpythonize_num = pyfloat_asdouble

(*-----------------------------------------------------------------------*)
